package com.liqiye.paymanager.controller;

import com.liqiye.paymanager.config.PaymentProperties;
import com.liqiye.paymanager.service.payment.StripePaymentService;
import com.stripe.model.identity.VerificationSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping()
public class ViewController {

    @Autowired
    private PaymentProperties config;
    @Autowired
    private StripePaymentService service;


    @GetMapping("/view")
    public ResponseEntity<?> view(){
        return ResponseEntity.ok("index");
    }

    /**
     * stripe 单次支付的页面
     */
    @GetMapping("/stripe/pay")
    public String stripePayView(Model model) {
        model.addAttribute("stripePublicKey", config.getStripe().getPublicKey());
        return "stripe_pay";
    }

    /**
     * stripe 订阅付款（自动付款）的页面
     */
    @GetMapping("/stripe/pay/auto")
    public String stripePayAutoView(Model model) {
        model.addAttribute("stripePublicKey", config.getStripe().getPublicKey());
        return "stripe_pay_auto";
    }

    /**
     * stripe 官方提供的对用户的身份验证的页面（风控处理）
     *
     * 这个 userId 参数是我们业务系统的用户id，传给 stripe 是为了：
     * Stripe 会把这个值存入 session.metadata.user_id 字段中，以便你在验证成功的回调（webhook）里可以知道：是谁完成了验证
     */
    @GetMapping("/stripe/identity")
    public String identityPage(String userId, Model model) {
        try {
            // 根据 userId 查询业务系统的用户，判断数据库中该用户的状态（是否是绿色用户，还是存在风险的用户）
            // 新创建的用户（我们业务系统的）默认状态是未确认，第一次付款时，这里先判断要验证，后续就不用再验证

            VerificationSession session = service.createVerificationSession(userId);
            model.addAttribute("verificationUrl", session.getUrl());
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
        }
        return "stripe_identity";
    }

    /**
     * airwallex的付款页面
     */
    @GetMapping("/airwallex")
    public String airwallex(Model model) {
        return "airwallex_pay";
    }



    /**
     * paypal-acdc-ap 页面
     */
    @GetMapping("/paypal/acdc/ap")
    public String paypalacdcap(Model model) {
        String clientId = config.getPaypal().getClientId();

        model.addAttribute("clientId", clientId);
        model.addAttribute("currency", "USD");
        model.addAttribute("buyerCountry", "US");
        model.addAttribute("merchantId", "VCWTVM8NL8SDA");
        return "paypal_acdc_ap";
    }

    /**
     * paypal-acdc-gp 页面
     */
    @GetMapping("/paypal/acdc/gp")
    public String paypalacdcgp(Model model) {
        String clientId = config.getPaypal().getClientId();

        model.addAttribute("clientId", clientId);
        model.addAttribute("currency", "USD");
        model.addAttribute("buyerCountry", "US");
        model.addAttribute("merchantId", "VCWTVM8NL8SDA");
        return "paypal_acdc_gp";
    }




    /**
     * paypal-acdc 卡 的付款页面
     */
    @GetMapping("/view/paypal")
    public String paypalAcdcPayment() {
        return "forward:/view/paypal_acdc_payment/index.html";
    }

    /**
     * paypal-acdc 卡 的付款页面
     */
    @GetMapping("/view/paypalsub")
    public String paypalAcdcSub() {
        return "forward:/view/paypal_acdc_sub/index.html";
    }

    @GetMapping("/view/paypalsub2")
    public String paypalAcdcSub2() {
        return "forward:/view/paypal_acdc_sub2/index.html";
    }

    @GetMapping("/view/paypalacdccardpreauth")
    public String paypalAcdcCardPreauth() {
        return "forward:/view/paypal_acdc_card_preauth/index.html";
    }

    @GetMapping("/view/paypalacdcap")
    public String paypalacdcap() {
        return "forward:/view/paypal_acdc_ap/index.html";
    }


}
